/**
 *
 * User: c3t
 * Date: 14-1-21
 * Time: 下午12:46
 */
/**
 <div>
 <ul class="navSelected">
 <h3>标题</h3>
 <li></li>
 <li></li>
 <li></li>
 </ul>
 </div

 <script>
 $(“div”).myaccordion();
 </script>
 */
define(function (require, exports, module) {
    return function (jQuery) {
        (function ($) {

            //检查ajax返回是成功还是失败
            $.ajaxCheck=function(data) {
                //data.result 返回1表示正确
                if(data.result){
                    return true;
                }else{
                 alert(data.msg);
                    return false;
                }
            };


            //表格排序功能
            $.fn.mySortTable=function(opts) {
                var _issort = false;
                var me=this;
                var sortEle = this.find("tbody");
                var setting = $.extend({
                    begin:"beginOrder",
                    save:"saveOrder"
                },opts ||{});
                sortEle.sortable({
                    axis: "y",
                    helper: function (e, ele) {
                        //ele is jquery object
                        // _original is Object[td, td.ctype, td, td, td, td.centerTd]
                        var _original = ele.children();
                        //_original.eq(0) is [Object object]
                        var _helper = ele.clone();
                        _helper.children().each(function (index) {
                            //这里面this不是jquery对象
                            $(this).width(_original.eq(index).width());
                        });
                        _helper.css("background", "blue");
                        return _helper;
                    },
                    update: function (e, ui) {
                        //拖动完之后,调用serOrders，重置表格的每一行的序号
                        setOrders();
                    }
                });

                sortEle.sortable("disable");

                $("#"+setting.begin).click(beginOrders);
                $("#"+setting.save).click(saveOrders);

                function beginOrders(event) {
                    event.preventDefault();
                    //_issort默认是false,第一次if语句代码会执行,当if代码执行后_issort为true就不会再查询一列序号，拖动排序td
                    if (!_issort) {
                        me.find("thead tr").append("<td>序号</td>");
                        setOrders();
                        me.find("tfoot tr").append("<td>拖动排序</td>");
                        _issort = true; //必须在插入默认排序后再设置_issort=true
                        sortEle.sortable("enable");
                    } else {
                        alert("已经处于排序状态");
                    }
                }

                function saveOrders(event) {

                    event.preventDefault();
                    if (_issort) {
                        var ids = sortEle.sortable("serialize", {key: "ids"});
                        console.info(ids);
                        $.post("admin/channel/channels/updateSort?" + ids, function (data) {
                            if($.ajaxCheck(data)) {
                                me.find("tr").each(function () {
                                    /*$(this).find("td:last").remove();*/
                                    $(this).children().last().remove();
                                });
                                sortEle.sortable("disable");
                                _issort = false;
                            }else{
                                alert(data.msg);
                            }
                        });

                    } else {
                        alert("不是排序状态不可存储");
                    }
                }

                function setOrders() {

                    me.find("tbody tr").each(function (index) {
                        if (_issort) {
                            $(this).find("td:last").html((index + 1));
                        } else {
                            $(this).append("<td>" + (index + 1) + "</td>");
                        }

                    });
                }

                return sortEle;
            };


            //页面accordion插件，页面左端菜单栏
            $.fn.myaccordion = function (opts) {
                var settings = $.extend({
                    selectedClz: "navSelected",
                    titleTagName: "h3"
                }, opts || {});
                //获得选中的标签
                var selectedNode = this.find("ul." + settings.selectedClz + ">" + settings.titleTagName);
                //获得所有标题节点<h3/>
                var titleNode = this.find("ul>" + settings.titleTagName);
                titleNode.css("cursor", "pointer");
                titleNode.nextAll().css("display", "none");
                selectedNode.nextAll().css("display", "block");
                titleNode.click(function () {
                    var checked = $(this).parent().hasClass(settings.selectedClz);
                    if (checked) {
                        $(this).nextAll().slideUp();
                        $(this).parent().removeClass(settings.selectedClz);
                    } else {
                        $(this).nextAll().slideDown();
                        $(this).parent().addClass(settings.selectedClz);
                    }
                });
            };

            //鼠标经过颜色改变
            $.fn.trColorChange = function (opts) {
                var settings = $.extend({
                    overClz: "trMouseover",
                    evenClz: "trEvenColor"
                }, opts || {});
                this.find("tbody tr:even").addClass(settings.evenClz);
                this.find("tbody tr").on("mouseenter mouseleave", function () {
                    $(this).toggleClass(settings.overClz);
                });
            };
            //提示操作不可恢复
            $.fn.confirmOperator = function (opts) {
                var settings = $.extend({
                    msg: "该操作不可逆,确定要操作么?",
                    evenName:"click"
                }, opts || {});
                this.on(settings.evenName,function(event){
                    if(!confirm(settings.msg)){
                        event.preventDefault();
                    }
                })

            };
        })(jQuery);
    };
});
